package com.shuoyue.ycgk.ui.course.mycourse;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.shuoyue.ycgk.R;
import com.shuoyue.ycgk.appcatch.SPUtils;
import com.shuoyue.ycgk.base.BaseMvpFragment;
import com.shuoyue.ycgk.base.baseadapter.IndexFragmentStateAdapter;
import com.shuoyue.ycgk.entity.Type;
import com.shuoyue.ycgk.event.SwitchBaseEvent;
import com.shuoyue.ycgk.ui.common.CommonModuleContract;
import com.shuoyue.ycgk.ui.news.FragmentNewsTab;
import com.shuoyue.ycgk.views.dialog.TypeSelectDialog;
import com.shuoyue.ycgk.views.viewpager_scroll.CustomScrollViewPager;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class FragmentMyCourse extends BaseMvpFragment implements CommonModuleContract.View {

    List<Type> baseTypes;//三大基类
    TypeSelectDialog typeSelectDialog;
    CommonModuleContract.Presenter comPresenter;


    public IndexFragmentStateAdapter indexFragmentStateAdapter;
    @BindView(R.id.type)
    TextView type;
    @BindView(R.id.search)
    ImageView search;
    @BindView(R.id.viewPager)
    CustomScrollViewPager viewPager;

    public static FragmentMyCourse getInstance() {
        FragmentMyCourse fragment = new FragmentMyCourse();
        return fragment;
    }

    @Override
    public void initParams(Bundle bundle) {

    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_main_news_type;
    }

    @Override
    protected void initView() {

    }

    @Override
    protected void initData() {
        comPresenter = new CommonModuleContract.Presenter();
        comPresenter.attachView(this);
        comPresenter.getBaseModels(4);
    }

    @OnClick({R.id.type, R.id.search})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.type:
                if (this.baseTypes == null || this.baseTypes.size() == 0) {
                    return;
                }
                if (typeSelectDialog == null) {
                    typeSelectDialog = new TypeSelectDialog(mContext, baseTypes);
                }
                typeSelectDialog.setData(baseTypes);
                typeSelectDialog.setCallback(baseType -> {
                    type.setText(baseType.getName());
//            comPresenter.getChildType(baseType.getEpId());
                    viewPager.setCurrentItem(baseTypes.indexOf(baseType));
                    EventBus.getDefault().post(new SwitchBaseEvent(baseType.getName()));
                });
                typeSelectDialog.show();
                break;
            case R.id.search:
                break;
        }
    }

    @Override
    public void setBaseType(List<Type> baseTypes) {
        this.baseTypes = baseTypes;
        List<BaseMvpFragment> fragmentMainItems = new ArrayList<>();
        for (Type topType : baseTypes) {
            fragmentMainItems.add(FragmentNewsTab.getInstance(topType));
        }
        indexFragmentStateAdapter = new IndexFragmentStateAdapter(getChildFragmentManager(), fragmentMainItems);
        viewPager.setAdapter(indexFragmentStateAdapter);
        viewPager.setOffscreenPageLimit(1);
        String perName = SPUtils.getLastSelectMoudle(mContext);
        if (perName != null) {
            for (Type baseType : baseTypes) {
                if (perName.equals(baseType.getName())) {
                    type.setText(baseType.getName());
                    type.setSelected(true);
                    viewPager.setCurrentItem(baseTypes.indexOf(baseType));
                }
            }
        } else {
            baseTypes.get(0).setSelect(true);
            type.setText(baseTypes.get(0).getName());
            type.setSelected(true);
            viewPager.setCurrentItem(0);
        }

    }

    @Override
    public void setTabType(List<Type> type) {

    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void switchBaseTypeEvent(SwitchBaseEvent s) {
        if (baseTypes != null) {
            if ("教师招聘".equals(s.getName())
                    || "公务员".equals(s.getName())
                    || "事业单位".equals(s.getName())) {
                for (Type baseType : baseTypes) {
                    baseType.setSelect(false);
                }
                for (Type baseType : baseTypes) {
                    if (s.getName().equals(baseType.getName())) {
                        baseType.setSelect(true);
                        type.setText(baseType.getName());
                        viewPager.setCurrentItem(baseTypes.indexOf(baseType));
                        break;
                    }
                }
            }
        }
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        EventBus.getDefault().register(this);
        return super.onCreateView(inflater, container, savedInstanceState);
    }

    @Override
    public void onDestroyView() {
        EventBus.getDefault().unregister(this);
        super.onDestroyView();
    }

}
